import gradio as gr
from spritesheet_generator import create_spritesheet
import os
import tempfile


def generate_spritesheet(video_file):
    """
    处理上传的视频文件并生成精灵表
    """
    if video_file is None:
        return None
    
    # 创建临时文件路径
    with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:
        output_path = tmp_file.name
    
    try:
        # 调用精灵表生成函数
        result_path = create_spritesheet(video_file.name, output_path)
        return result_path
    except Exception as e:
        print(f"处理视频时出错: {str(e)}")
        return None


# 创建Gradio界面
description = """
# 视频到精灵表转换器
上传一个视频文件，系统将自动生成一张包含所有视频帧的PNG精灵表。
"""

iface = gr.Interface(
    fn=generate_spritesheet,
    inputs=gr.File(label="上传视频", file_types=[".mp4", ".avi", ".mov", ".mkv"]),
    outputs=gr.File(label="下载精灵表"),
    title="SpritesGen - 精灵表生成器",
    description=description,
    examples=None,
    allow_flagging="never"
)

if __name__ == "__main__":
    iface.launch()



